package com.distribute.framework.plugin;

import java.util.Date;

import com.distribute.framework.model.SrvError;

public class Error {
	
	public static void writeError(Throwable throwable,String typeFlag,String strData) {
		try{
			if(throwable.getCause()!=null)throwable = throwable.getCause();
			String strStack = ""; 
			for(StackTraceElement stackTraceElement:throwable.getStackTrace()){
				if(stackTraceElement.toString().indexOf("com.distribute")>-1){
					strStack+=stackTraceElement.toString()+"("+stackTraceElement.getFileName()+":"+stackTraceElement.getLineNumber()+")\n";
				}
			}
			String strTitle = throwable.getClass().getName()+":"+throwable.getMessage()+" - "+throwable.getStackTrace()[0].toString();
			SrvError srvError = SrvError.where.seTitle(strTitle).get();
			if(srvError==null){
				srvError = new SrvError(
					strTitle//错误标题 [非空]
				 	,strStack//错误内容
				 	,typeFlag//标识
				 	,1//错误次数
				 	,new Date()//修改时间 [非空]
				 	,new Date()//添加时间 [非空]
				 	,(short)1//状态 [非空]
				 ).save();
			}else{
				SrvError.where.seId(srvError.getId()).set.seUpdateTime(new Date()).definex("se_count=se_count+1").update();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}